require("../less/infochange.less");
document.ready(function() {

    // axios设置服务器地址
    axios.defaults.baseURL = "http://139.9.177.51:8099"; //服务器地址


    let sexText = document.querySelector('#sexText');
    let birText = document.querySelector('#birText');
    let proText = document.querySelector('#proText');
    let cityText = document.querySelector('#cityText');
    let signText = document.querySelector('#signText');
    let petName = document.querySelector('#js_input1');

    //初始化信息界面
    initialization();

    function initialization() {
        axios.get("/users/accountinfo?userId=" + localStorage.getItem('userId'))
            .then(function(res) {
                console.log(res.data.data);
                if (res.data.status == 0) {

                    // 里面数据不为null时再执行
                    //头像
                    let imgurl = res.data.data.imgurl;
                    if (imgurl) {
                        profilePhoto.src = imgurl;
                    }
                    //昵称
                    let nickname = res.data.data.nickname;
                    if (nickname) {
                        petName.value = nickname;
                    }
                    // 性别
                    let gender = res.data.data.gender;
                    if (gender) {
                        sexText.textContent = gender;
                    }
                    //生日
                    let birthday = res.data.data.birthday;

                    if (birthday) {
                        birText.textContent = birthday.split('T')[0];
                    }
                    //地址

                    let address = res.data.data.address;
                    if (address) {
                        address = address.split(',');
                        proText.textContent = address[0];
                        cityText.textContent = address[1];
                    }
                    //个性签名
                    let sign = res.data.data.sign;
                    if (sign) {
                        signText.textContent = sign;
                    }
                }
            })
            .catch(function(err) {
                console.log(err);
            })

    }









    //返回上一页
    document.querySelector('#backprev').addEventListener('click', function() {
        history.back();
    })

    //选择性别
    document.querySelector('#sexPicker').addEventListener('click', function() {
        weui.picker([{
            label: '女',
            value: 0
        }, {
            label: '男',
            value: 1
        }], {
            onConfirm: function(result) {
                document.querySelector('#sexText').textContent = result[0].label;
            },
            title: '请选择性别'
        });
    });

    //省份和城市
    let arrPro = []; //保存省份全局变量
    let arrCity = []; //保存城市全局变量

    //获取省份信息
    getPro();

    function getPro() {
        axios.get('/address/province')
            .then(function(res) {
                arrPro = res.data.data.map(function(v) {
                    return { label: v.name, value: v.addressId }
                })
            })
            .catch(function(err) {
                console.log(err);
            })
    }

    //选择省份 
    //选择省份之后选择城市
    document.querySelector('#proPicker').addEventListener('click', function() {
        weui.picker(arrPro, {
            onConfirm: function(result) {
                proText.textContent = result[0].label;


                //根据省份获取城市 pid 就是value
                let pid = result[0].value;
                axios.get("/address/city/" + pid)
                    .then(function(res) {
                        arrCity = res.data.data.map(function(v) {
                                return { label: v.name, value: v.addressId }
                            })
                            // 将城市的默认值设为省会城市
                        cityText.textContent = arrCity[0].label;

                        console.log(cityText.textContent);

                    })
                    .catch(function(err) {
                        console.log(err);
                    })
            },
            title: '请选择省份'
        });
    });


    //选择城市
    document.querySelector('#cityPicker').addEventListener('click', function() {
        weui.picker(arrCity, {
            onConfirm: function(result) {
                cityText.textContent = result[0].label;
            },
            title: '请选择城市'
        });
    });



    //选择生日
    document.querySelector('#birPicker').addEventListener('click', function() {
        weui.datePicker({
            start: new Date().getFullYear() - 100,
            end: new Date().getFullYear() - 12,
            onConfirm: function(result) {
                birText.textContent = [result[0].value, window.$utils.fillZero(result[1].value), window.$utils.fillZero(result[2].value)].join('-');
            },
            title: '请选择生日'
        });
    });


    let picture = null; //头像的全局变量
    //跟换头像 将头像上传到后端
    document.querySelector('#userImgIpt').addEventListener('change', function() {
        //获取上传的图片信息this.files[0]  files 只针对type="file"

        //创建一个内置对象 formdata
        let fm = new FormData();
        //追加参数到formdata对象中
        fm.append('imgurl', this.files[0]); //键名:键值 键值还是一个对象
        //只能这样写,因为fm就是一个对象
        axios.post("/users/upload", fm)
            .then(function(res) {
                //上传成功时
                if (res.data.status == 0) {
                    picture = 'http://139.9.177.51:8099' + res.data.data;
                    document.querySelector('#profilePhoto').src = picture;
                }
            })
            .catch(function(err) {
                console.log(err);
            })
    })


    //将profilePhoto的change事件转移给userImgIpt
    document.querySelector('#profilePhoto').addEventListener('click', function() {
        //userImgIpt其实是隐藏的一个点击事件,点击userImgIpt 就会弹出选择文件框
        document.querySelector('#userImgIpt').click();
    })


    //给保存按钮添加点击事件 实现保存功能
    document.querySelector('#saveBtn').addEventListener('click', function() {

        let params = {
            imgurl: picture,
            nickname: petName.value,
            gender: sexText.textContent,
            birthday: birText.textContent,
            address: [proText.textContent, cityText.textContent],
            sign: signText.value,
            userId: localStorage.getItem('userId')
        };
        console.log(params);
        axios.post("/users/userEdit", params)
            .then(function(res) {
                if (res.data.status == 0) {
                    window.$utils.tipsFn('保存数据成功', res.data.status);
                    setTimeout(function() {
                        location.href = './my.html';
                    }, 2000);

                } else {
                    window.$utils.tipsFn('请稍后重试', res.data.status);
                }
            })
            .catch(function(err) {
                console.log(err);
            })


    })



})